//
//  PhoneticReadingToolBar.m
//  ebook
//
//  Created by mt on 16/10/26.
//  Copyright © 2016年 Moton. All rights reserved.
//

/*
 *调节语音阅读的语速toolBar
 */

#define ReadingToolBarHeight 80
#define BtnWidth (MTScreenW-40)/3
#define BtnHeight 40
#define reduceTitle   @"-减速"
#define increaseTitle @"+加速"
#define kDefaultText @"当前速度"

#import "PhoneticReadingToolBar.h"
#import "ReaderGlobalModel.h"

@interface PhoneticReadingToolBar ()
{
    UILabel *tempLabel;
}
@end

@implementation PhoneticReadingToolBar

- (instancetype)init {
    
    self = [super initWithFrame:CGRectMake(0, MTScreenH-ReadingToolBarHeight, MTScreenW, ReadingToolBarHeight)];
    self.backgroundColor = MTConfigColor(@"MTReaderToolBar_Bg_Color");
    if (self = [super init]) {
        
        [self drawView];
    }
    return self;
}

- (void)drawView {
    
    UIButton *reduceBtn = [self btnWithFrame:CGRectMake(20, 20, BtnWidth, BtnHeight) title:reduceTitle tag:1];
    [self addSubview:reduceBtn];
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(reduceBtn.frame), CGRectGetMinY(reduceBtn.frame), BtnWidth, BtnHeight)];
    textLabel.attributedText = [self stringColorWithString:[NSString stringWithFormat:@"%@% ld",kDefaultText,[ReaderGlobalModel shareModel].readerConfig.phoneticReadingSpeed]];
    textLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:textLabel];
    tempLabel = textLabel;
    
    UIButton *increaseBtn = [self btnWithFrame:CGRectMake(MTScreenW-CGRectGetMaxX(reduceBtn.frame), CGRectGetMinY(reduceBtn.frame), BtnWidth, BtnHeight) title:increaseTitle tag:2];
    [self addSubview:increaseBtn];
}

- (NSAttributedString *)stringColorWithString:(NSString *)str {
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] initWithString:str];
    //当前速度 1
    //字号
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont systemFontOfSize:14.0]
                          range:NSMakeRange(0, str.length)];
    //当前速度
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:MTConfigColor(@"MTReaderToolBar_textTint_Color")
                          range:NSMakeRange(0, str.length-1)];
    //1
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:[UIColor redColor]
                          range:NSMakeRange(str.length-1, 1)];
    return AttributedStr;
}

- (UIButton *)btnWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:MTConfigColor(@"MTReaderToolBar_textTint_Color") forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
    btn.layer.borderColor = MTConfigColor(@"MTReaderToolBar_btnSelected_Color").CGColor;
    btn.layer.borderWidth = 1.0f;
    btn.layer.cornerRadius = 3.0f;
    
    btn.tag = tag;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (void)btnClick:(UIButton *)btn {
    
    int speed = [self speed];
    if (1==btn.tag) {
        if (speed>1) {
            speed--;
        } else {
            return;
        }
    }
    if (2==btn.tag) {
        if (speed<5) {
            speed++;
        } else {
            return;
        }
    }
    [self resetReadingSpeed:speed];
    
    if ([self.delegate respondsToSelector:@selector(phoneticReadingToolBar:readSpeedAccelerated:)]) {
        [self.delegate phoneticReadingToolBar:self readSpeedAccelerated:2==btn.tag];
    }
}

- (void)resetReadingSpeed:(int)speed {
    
    [ReaderGlobalModel shareModel].readerConfig.phoneticReadingSpeed = speed;
    NSString *str = [NSString stringWithFormat:@"%@ %d", kDefaultText, speed];
    tempLabel.attributedText = [self stringColorWithString:str];
}

- (int)speed {
    
    return [[tempLabel.text componentsSeparatedByString:kDefaultText].lastObject intValue];
}

-(void)showOnView:(UIView *)view {
   
    [UIView animateWithDuration:0.3 animations:^{
        CGRect origFrame = self.frame;
        origFrame.origin.y = MTScreenH - CGRectGetHeight(origFrame);
        self.frame = origFrame ;
        [view addSubview:self];
        [view bringSubviewToFront:self];
    }];
}

-(void)hide {
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect origFrame = self.frame;
        origFrame.origin.y = origFrame.origin.y + CGRectGetHeight(origFrame);
        self.frame = origFrame ;
    }];
}

-(void)setIsNight:(BOOL)isNight{
    
    _isNight = isNight;
    
    self.backgroundColor =  MTConfigColor(_isNight?@"MTReaderBar_NightBg_Color":@"MTReaderBar_DayBg_Color");
}

@end
